package com.work.keep.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.work.keep.R;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.adapter.MyFollowsRecyclerAdapter;
import com.work.keep.adapter.MyOtherUserListViewAdapter;
import com.work.keep.adapter.OtherUserDynamicAdapter;
import com.work.keep.base.BaseFragment;
import com.work.keep.datas.FragmentOtherDynamicData;
import com.work.keep.view.MyListView;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://api.gotokeep.com/v1.1/people/56999b2d9951948b57b2c53b/photos?limit=18
 * /photos?limit=18
 * Created by dllo on 16/1/18.
 */
public class OtherUserDynamicFragment extends BaseFragment {
    private Context context;
    private RecyclerView myRecyclerView;
    VolleySingleton volleySingleton;
    ImageLoader imageLoader;
    private OtherUserDynamicAdapter otherUserDynamicAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_other_user_dynamic, container, false);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myRecyclerView = (RecyclerView) view.findViewById(R.id.OtherUserRecyclerView);
    }

    @Override
    protected void init() {
        String url = getArguments().get("url") + "/photos?limit=18";
        otherUserDynamicAdapter = new OtherUserDynamicAdapter();
        myRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        myRecyclerView.setAdapter(otherUserDynamicAdapter);
        StringRequest otherDynamic = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                FragmentOtherDynamicData data = JSON.parseObject(response, FragmentOtherDynamicData.class);
                otherUserDynamicAdapter.addDataLists(data.getData());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> dynamicHeader = new HashMap<>();
                dynamicHeader.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzE2NDEwNCwiZXhwIjoxNDU1NzU2MTA0LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.iSPkGXPiR7VG_Fj-z7YDCj2_uuLbecau5bFoUU0iB10");
                dynamicHeader.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                dynamicHeader.put("Host", "api.gotokeep.com");
                dynamicHeader.put("If-None-Match", "W/\"d31-zBP8NgaJJvecQ0qMhgeVfw\"");
                return dynamicHeader;
            }
        };
        volleySingleton._addRequest(otherDynamic);

    }
}
